package conson.tech.manage.scene.service.impl;

import cn.hutool.core.lang.Dict;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import conson.tech.core.constant.CommonConstants;
import conson.tech.manage.scene.entity.Dictionary;
import conson.tech.manage.scene.mapper.DictionaryMapper;
import conson.tech.manage.scene.service.DictionaryService;
import conson.tech.manage.scene.vo.DictionaryVO;
import conson.tech.manage.scene.vo.request.DictionaryRequest;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * @Description: 字典服务实现
 * @author: yuwenshuo
 * @since: 2020-06-22 09:45
 */

@Service
public class DictionaryServiceImpl implements DictionaryService {

    @Autowired
    private DictionaryMapper dictionaryMapper;

    @Override
    @Transactional
    public void save(DictionaryRequest dictionaryRequest, String operationUser) {

        Integer id = dictionaryRequest.getId();

        if(id != null){
            Dictionary dictionary = dictionaryMapper.selectById(id);
            dictionary.setVal(dictionaryRequest.getVal());
            dictionary.setRemark(dictionaryRequest.getRemark());
            dictionary.setUpdatedTime(LocalDateTime.now());
            dictionary.setUpdatedBy(operationUser);
            dictionaryMapper.updateById(dictionary);
            return;
        }


        Dictionary dictionary = this.buildPO(dictionaryRequest, operationUser);
        dictionaryMapper.insert(dictionary);
    }


    @Override
    @Transactional
    public void delete(Integer dictionaryId, String operationUser) {
        Dictionary updateParam =  new Dictionary();
        updateParam.setId(dictionaryId);
        updateParam.setUpdatedTime(LocalDateTime.now());
        updateParam.setUpdatedBy(operationUser);
        updateParam.setDelFlag(CommonConstants.DEL_FLAG_DEL);
        dictionaryMapper.updateById(updateParam);
    }

    @Override
    public void lock(Integer dictionaryId, String operationUser) {
        Dictionary updateParam = new Dictionary();
        updateParam.setId(dictionaryId);
        updateParam.setUpdatedTime(LocalDateTime.now());
        updateParam.setUpdatedBy(operationUser);
        updateParam.setState(CommonConstants.DEL_FLAG_DEL);
        dictionaryMapper.updateById(updateParam);
    }

    @Override
    public void unlock(Integer dictionaryId, String operationUser) {
        Dictionary updateParam = new Dictionary();
        updateParam.setId(dictionaryId);
        updateParam.setUpdatedTime(LocalDateTime.now());
        updateParam.setUpdatedBy(operationUser);
        updateParam.setState(CommonConstants.DEL_FLAG_NORMAL);
        dictionaryMapper.updateById(updateParam);
    }

    @Override
    public List<DictionaryVO> getDictionaryByParam(DictionaryRequest queryParam) {
        Dictionary query = new Dictionary();
        BeanUtils.copyProperties(queryParam, query);
        query.setDelFlag(CommonConstants.DEL_FLAG_NORMAL);

        List<Dictionary> dataList = dictionaryMapper.selectList(Wrappers.<Dictionary>query().lambda().setEntity(query));

        List<DictionaryVO> resList = new ArrayList<>();

        if(CollectionUtils.isEmpty(dataList)){
            return resList;
        }


        dataList.stream().forEach(data -> resList.add(new DictionaryVO(data)));

        return resList;

    }


    @Override
    public DictionaryVO getDictionaryByCondition(DictionaryRequest queryParam) {
        Dictionary query = new Dictionary();
        BeanUtils.copyProperties(queryParam, query);
        query.setState(CommonConstants.DEL_FLAG_NORMAL);
        query.setDelFlag(CommonConstants.DEL_FLAG_NORMAL);

        Dictionary dictionary = dictionaryMapper.selectOne(Wrappers.<Dictionary>query().lambda().setEntity(query));

        return new DictionaryVO(dictionary);
    }



    private Dictionary buildPO(DictionaryRequest request, String operationUser){
        Dictionary dictionary = new Dictionary();
        dictionary.setVal(request.getVal());
        dictionary.setRemark(request.getRemark());
        dictionary.setScene(request.getScene());
        dictionary.setType(request.getType());
        dictionary.setId(request.getId());
        dictionary.setCreatedBy(operationUser);
        dictionary.setCreatedTime(LocalDateTime.now());
        dictionary.setUpdatedBy(operationUser);
        dictionary.setUpdatedTime(LocalDateTime.now());
        dictionary.setDelFlag(CommonConstants.DEL_FLAG_NORMAL);
        dictionary.setState(CommonConstants.DEL_FLAG_NORMAL);
        return dictionary;

    }
}
